#!/usr/bin/env python3
# coding=utf-8
# Created by han on 2016/5/20
import pickle,os,time

frist_page=["购买物品","购买历史","购物车","充值"]

rechange_page=["余额查询","充值"]

user_home='normal_user.txt'
user_lock='lock_user.txt'
cart='cart_user.txt'
user_buy='buy_user.txt'

vegetable=[
    ("西红柿",3),
    ("茄子",2),
    ("豆角",4),
    ("白菜",1)
    ]

fruit=[
    ("香蕉",6),
    ("苹果",4),
    ("橙子",5),
    ("甜瓜",6)
    ]


while True:
    print('''
        欢迎来到宇宙超市请选择：
        ======================
        1.注册账号
        2.用户登陆
        3.关于作者
        ======================
    ''')
    user_input=input("请输入您的选择：")
    if user_input == '1':
        print('''
             菜单：注册账号
             ==============
             1.注册账号
             2.返回
             ==============
        ''')
        sign_input=input("请输入您的选择：")
        if sign_input=='1':                 #如果是1则进入注册流程
            while True:
                input_user=input("请输入要注册的【用户名】或者输入【b】退出:")
                if input_user=='b':             #如果输入b则退出注册
                    break
                else:
                    if os.path.exists(user_home):                 #如果用户信息存放文件存在
                        with open(user_home,'rb') as user_file:   #调用pickle对字典进行序列化处理
                            user_dict=pickle.load(user_file)                #读取文件中的字典
                        if input_user in user_dict:                         #判断用户名是否在字典中
                            print("[%s]此账户已经被注册"%(input_user))        #在
                        else:                                               #不在
                            input_pass=input('请输入密码: ')
                            with open(user_home,'rb') as user_file:       #读取已经有的字典并将用户名密码追加到字典中
                                user_dict=pickle.load(user_file)                    #创建用户信息字典
                                user_dict[input_user]={}
                                user_dict[input_user]['have_money']=0
                                #user_dict[input_user]['out_money']=0
                                #user_dict[input_user]['mer']={}
                                user_dict[input_user]['pass']=input_pass
                                with open(user_home,'wb') as newuser_list:    #将新追加用户名的字典序列化存入到文件中
                                    pickle.dump(user_dict,newuser_list)
                            with open(user_lock,'rb') as lockr_file:           #读取锁定文件
                                lock_dict=pickle.load(lockr_file)
                                lock_dict[input_user] = 3
                            with open(user_lock,'wb') as newlockw_file:         #向锁定文件中添加新用户
                                pickle.dump(lock_dict,newlockw_file)

                            with open(user_home, 'rb') as getuser_file:         # 获取用户账户中的金额
                                getuser_dict=pickle.load(getuser_file)
                                get_money=getuser_dict[input_user]['have_money']
                            with open(cart,'rb') as cartr_file:
                                cart_dict=pickle.load(cartr_file)
                                cart_dict[input_user]={}
                                cart_dict[input_user]['mer']={}
                                cart_dict[input_user]['mon']=get_money         #把获取到的用户中账户金额放到购物车中
                                cart_dict[input_user]['cartmon']=0
                            with open(cart,'wb') as cartw_file:
                                pickle.dump(cart_dict,cartw_file)

                            with open(user_buy,'rb') as buyr_file:      #把信息读出来然后赋值
                                buy_dict=pickle.load(buyr_file)
                                buy_dict[input_user]={}
                                buy_dict[input_user]['mer']={}
                                buy_dict[input_user]['out_money']=0

                            with open(user_buy,'wb') as buyw_file:      #把赋值后的字典存放到购买文件中
                                pickle.dump(buy_dict,buyw_file)

                            print('''
                                ============================
                                    注册成功，请继续您的操作。
                                ============================
                                    ''')
                            continue
                    else:                                                               #如果用户信息文件不存在
                        state={}                        #用户
                        state[input_user]={}
                        state[input_user]['have_money'] = 0
                        count=3
                        cart_dict={}                                                #用户购物车信息初始化
                        cart_dict[input_user] = {}
                        cart_dict[input_user]['mer'] = {}
                        cart_dict[input_user]['mon'] = 0
                        cart_dict[input_user]['cartmon'] = 0

                        buy_dict={}                                         #用户已购买信息初始化  ##需要修改查看已购买记录
                        buy_dict[input_user]={}
                        buy_dict[input_user]['mer']={}
                        buy_dict[input_user]['out_money']=0

                        with open(user_home,'wb') as statew_file:           #初始化用户信息文件。
                            pickle.dump(state,statew_file)
                        with open(user_home, 'rb') as stater_file:          #读取用户信息文件初始化信息
                            user_dict=pickle.load(stater_file)

                        with open(user_lock,'wb') as lockw_file:            #初始化锁信息文件
                            pickle.dump(state,lockw_file)
                        with open(user_lock, 'rb') as lockr_file:           #读取锁信息文件初始化信息
                            lockr_dict=pickle.load(lockr_file)

                        with open(cart,'wb') as cartw_file:                 #初始化购物车文件
                            pickle.dump(cart_dict,cartw_file)

                        with open(user_buy,'wb') as buyr_file:              #初始化已购买文件
                            pickle.dump(buy_dict,buyr_file)

                        input_pass = input("请输入注册密码：")
                        user_dict[input_user]['pass']=input_pass            #将用户名密码追加到字典中
                        lockr_dict[input_user]=count
                        with open(user_home, 'wb') as userww_file:
                            pickle.dump(user_dict, userww_file)
                        with open(user_lock,'wb') as lockww_file:
                            pickle.dump(lockr_dict,lockww_file)
                        print('''
                                   ============================
                                       注册成功，请继续您的操作。
                                   ============================
                                       ''')
                        continue
        elif sign_input!='1':
            continue
    elif user_input == '2':
        while True:
            print('''
            菜单：登陆菜单
             ==============
             1.用户登陆
             2.返回上层
             ==============
            ''')
            sign2_input=input('请输入您的选择：')
            if sign2_input=='1':
                while True:
                    user_name=input("请输入您的【用户名】或者输入【b】返回到上一层:")
                    if user_name=='b':
                        break
                    else:
                        if os.path.exists(user_lock):
                            with open(user_lock, 'rb') as lock_file:  # 检查这个用户是否已经被锁定
                                lock_dict = pickle.load(lock_file)
                            if user_name in lock_dict:
                                with open(user_lock, 'rb') as lock_file:  # 检查这个用户是否已经被锁定
                                    lock_dict = pickle.load(lock_file)
                                lock_count=lock_dict[user_name]
                                if lock_count!=0:                           #如果错误次数为不为0则提示输入密码
                                    user_pass=input("请输入密码： ")          #如果没有被锁定输入密码
                                    if os.path.exists(user_home):
                                        with open(user_home,'rb') as nor_file:
                                            nor_user=pickle.load(nor_file)
                                        if user_name not in nor_user:
                                            print('对不起没有这个用户。')
                                            continue
                                        elif nor_user[user_name]['pass']==user_pass:
                                            print('欢迎光临')
                                            lock_dict[user_name]=3
                                            with open(user_lock,'wb') as rlock_file:
                                                pickle.dump(lock_dict,rlock_file)
                                            #################################
                                            while True:
                                                welcome_msg = "欢迎光临宇宙超市".center(20, '-')
                                                print(welcome_msg)
                                                for i in range(len(frist_page)):  # 判断菜单数量，有几个行就遍历几次
                                                    frist_mer = frist_page[i]  # 把物品名字赋值
                                                    f = i + 1
                                                    print(f, frist_mer)  # 显示首页物品顺序和名称
                                                frist_choice = input("请选择：")  # 请用户选择第一页物品
                                                if frist_choice.isdigit() and int(frist_choice)<=len(frist_page):
                                                    #判断用户输入是否为数字并且数字不能超过列表的长度
                                                    frist_choice=int(frist_choice)
                                                    if frist_choice == 1:
                                                        exit_flag = False
                                                        while not exit_flag:                #如果标志位为真
                                                            mer_msg = "商品货架".center(20, '=')
                                                            print(mer_msg)
                                                            print('''
        1.蔬菜
        2.水果
                                                            ''')
                                                            mer = "=".center(22, '=')
                                                            print(mer)
                                                            second_choice = input("请输入您的选择或者【b】返回上一层【c】查看购物车：")
                                                            if second_choice.isdigit():                     #选择功能列表
                                                                second_choice = int(second_choice)
                                                                if second_choice == 1:                      #选择1为蔬菜
                                                                    #count=1
                                                                    # for veg in vegetable:
                                                                    #     print(count,',【',veg[0],':',veg[1],'】')
                                                                    #     count += 1
                                                                    choise_flag=False
                                                                    while not choise_flag:
                                                                        count = 1
                                                                        for veg in vegetable:
                                                                            print(count, ',【', veg[0], ':', veg[1], '】')
                                                                            count += 1
                                                                        third_choise = input("选择商品加入**购物车**或输入【b】返回上一层查看**购物车**：")
                                                                        if third_choise == "b":
                                                                            break
                                                                        third_num = input("数量：")
                                                                        ###添加判断输入是否为数字并且不能大于所有物品的数量
                                                                        if third_choise.isdigit() and third_num.isdigit():
                                                                            third_num=int(third_num)
                                                                            third_choise=int(third_choise)
                                                                            if third_choise==1:             #买西红柿
                                                                                c="西红柿"
                                                                                with open(cart,'rb') as cart_out:  #获取已放购物车列表
                                                                                    cart_out_dict=pickle.load(cart_out)
                                                                                    raw_mer=cart_out_dict[user_name]['mer'] #把这个人所有的物品列表都赋值给raw_mert)
                                                                                    raw_cartmon=cart_out_dict[user_name]['cartmon']   #原来的购物车总费用赋值
                                                                                    if c in raw_mer:
                                                                                        new_mer=raw_mer[c][0]+third_num         #最终累计的西红柿数量
                                                                                        new_mon=new_mer*3                       #累计的西红柿价格
                                                                                        raw_mer[c]=[new_mer,new_mon]
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        #print('---------')
                                                                                        veg_mon=vegetable[0][1]             #获取单个西红柿的价格
                                                                                        now_mon=third_num*veg_mon           #购买合计的西红柿价格
                                                                                        new_mer={c:[third_num,now_mon]}     #最终物品的添加数量
                                                                                        #print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                            elif third_choise== 2:
                                                                                c="茄子"
                                                                                with open(cart,'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name]['mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的西红柿数量
                                                                                        new_mon = new_num * 3  # 累计的西红柿价格
                                                                                        new_mer={c:[new_num,new_mon]}
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        veg_mon = vegetable[1][1]  # 获取单个西红柿的价格
                                                                                        now_mon = third_num * veg_mon  # 购买合计的西红柿价格
                                                                                        new_mer = {c: [third_num,now_mon]}  # 最终物品的添加数量
                                                                                        #print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                            elif third_choise== 3:
                                                                                c="豆角"
                                                                                with open(cart,'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name]['mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的西红柿数量
                                                                                        new_mon = new_num * 4  # 累计的豆角价格
                                                                                        new_mer = {c: [new_num, new_mon]}
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        veg_mon = vegetable[2][1]  # 获取单个豆角的价格
                                                                                        now_mon = third_num * veg_mon  # 购买合计的豆角价格
                                                                                        new_mer = {c:[third_num,now_mon]}  # 最终物品的添加数量
                                                                                        #print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                            elif third_choise == 4:
                                                                                c = "白菜"
                                                                                with open(cart,'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name]['mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的白菜数量
                                                                                        new_mon = new_num * 1  # 累计的白菜价格
                                                                                        new_mer = {c: [new_num, new_mon]}
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        veg_mon = vegetable[3][1]  # 获取单个白菜的价格
                                                                                        now_mon = third_num * veg_mon  # 购买合计的白菜价格
                                                                                        new_mer = {c: [third_num,now_mon]}  # 最终物品的添加数量
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        #print(cart_out_dict)
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                            else:
                                                                                choise_flag=True
                                                                        else:
                                                                            print("输入有误，请重新输入")
                                                                            continue
                                                                elif second_choice == 2:                    #选择2为水果
                                                                    choise_flag = False
                                                                    while not choise_flag:
                                                                        count = 1
                                                                        for fru in fruit:
                                                                            print(count, ',【', fru[0], ':', fru[1], '】')
                                                                            count += 1
                                                                        third_choise = input("选择商品加入**购物车**或输入【b】返回上一层查看**购物车**：")
                                                                        if third_choise == "b":
                                                                            break
                                                                        third_num = input("数量：")
                                                                        #print(type(third_choise),type(third_num))
                                                                        ###添加判断输入是否为数字并且不能大于所有物品的数量
                                                                        if third_choise.isdigit() and third_num.isdigit():
                                                                            third_num = int(third_num)
                                                                            third_choise = int(third_choise)
                                                                            if third_choise == 1:  # 香蕉
                                                                                c = "香蕉"
                                                                                with open(cart,'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name]['mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的香蕉数量
                                                                                        new_mon = new_num * 6  # 累计的香蕉价格
                                                                                        new_mer = {c: [new_num, new_mon]}
                                                                                        # print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        # print(cart_out_dict)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        fru_mon = fruit[0][1]  # 获取单个香蕉的价格
                                                                                        now_mon = third_num * fru_mon  # 购买合计的白菜价格
                                                                                        new_mer = {c: [third_num,now_mon]}  # 最终物品的添加数量
                                                                                        # print(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        #print(cart_out_dict)
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)

                                                                            elif third_choise == 2:  # 苹果
                                                                                c = "苹果 "
                                                                                with open(cart,'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name]['mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的白菜数量
                                                                                        new_mon = new_num * 4  # 累计的白菜价格
                                                                                        new_mer = {c: [new_num, new_mon]}
                                                                                        # print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        fru_mon = fruit[1][1]  # 获取单个香蕉的价格
                                                                                        now_mon = third_num * fru_mon  # 购买合计的白菜价格
                                                                                        new_mer = {c: [third_num,now_mon]}  # 最终物品的添加数量
                                                                                        # print(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        #print(cart_out_dict)
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)



                                                                            elif third_choise == 3:
                                                                                c = "橙子"
                                                                                with open(cart,'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name]['mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的橙子数量
                                                                                        new_mon = new_num * 5  # 累计的橙子价格
                                                                                        new_mer = {c: [new_num, new_mon]}
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        fru_mon = fruit[2][1]  # 获取单个橙子的价格
                                                                                        now_mon = third_num * fru_mon  # 购买合计的白菜价格
                                                                                        new_mer = {c: [third_num,now_mon]}  # 最终物品的添加数量
                                                                                        #print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)

                                                                            elif third_choise == 4:
                                                                                c = "甜瓜"
                                                                                with open(cart,
                                                                                          'rb') as cart_out:  # 获取已放购物车列表
                                                                                    cart_out_dict = pickle.load(cart_out)
                                                                                    raw_mer = cart_out_dict[user_name][
                                                                                        'mer']  # 把所有的物品列表都赋值给raw_mert)
                                                                                    if c in raw_mer:
                                                                                        new_num = raw_mer[c][0] + third_num  # 最终累计的橙子数量
                                                                                        new_mon = new_num * 6  # 累计的橙子价格
                                                                                        new_mer = {c: [new_num, new_mon]}
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)
                                                                                    else:
                                                                                        fru_mon = fruit[3][1]  # 获取单个橙子的价格
                                                                                        now_mon = third_num * fru_mon  # 购买合计的白菜价格
                                                                                        new_mer = {c: [third_num,now_mon]}  # 最终物品的添加数量
                                                                                        #print(new_mer)
                                                                                        cart_out_dict[user_name]['mer'].update(new_mer)
                                                                                        print("\n商品 数量")
                                                                                        print('''%s   %d\n---已加入购物---\n''' % (c, third_num))
                                                                                        with open(cart,'wb') as cart_in:  # 把新的购物车信息放到表里，替代原来的表
                                                                                            pickle.dump(cart_out_dict,cart_in)

                                                                            else:
                                                                                choise_flag = True
                                                                        else:
                                                                            print("输入有误，请重新输入")
                                                                            continue
                                                                    for fru in enumerate(fruit):
                                                                        print(fru[0]+1,fru[1])
                                                                else:                                       #其他则退出购物窗口
                                                                    exit2_flag=True
                                                            elif second_choice == 'c':
                                                                out_flag = False
                                                                while not out_flag:
                                                                    with open(user_home, 'rb') as homer_file:
                                                                        home_dict = pickle.load(homer_file)
                                                                        money = home_dict[user_name]['have_money']
                                                                    with open(cart, 'rb') as cartr_file:
                                                                        cartr_dict = pickle.load(cartr_file)
                                                                        # print(cartr_dict)
                                                                        cart_mon = 0
                                                                    with open(user_buy, 'rb') as buyr_file:
                                                                        buyr_dict = pickle.load(buyr_file)

                                                                        for user_mer in cartr_dict[user_name]['mer']:
                                                                            cart_mer = cartr_dict[user_name]['mer'][
                                                                                user_mer]
                                                                            print(user_mer, cart_mer)
                                                                            cart_mon += cart_mer[1]
                                                                        money_msg = ("您的余额为【%s】,购物车内物品金额【%s】" % (
                                                                        money, cart_mon)).center(30, '+')
                                                                        print('\n')
                                                                        print(money_msg)
                                                                        print('\n')
                                                                    cart_chiose = input("输入【y】结账，输入【c】清空看购物车，输入【b】退出")
                                                                    if cart_chiose.isalnum():
                                                                        cart_chiose = cart_chiose.strip()
                                                                        if cart_chiose == 'y':
                                                                            cartr_mer = cartr_dict[user_name]['mer']
                                                                            # print(cartr_mer)
                                                                            if cartr_mer != {}:
                                                                                buy_mer = buyr_dict[user_name]['mer']
                                                                                buy_time = time.time()
                                                                                local_time = time.localtime(buy_time)
                                                                                show_time = time.strftime(
                                                                                    '%Y-%m-%d %H:%M:%S', local_time)
                                                                                new_mer = {show_time: cartr_mer}
                                                                                buy_mer.update(new_mer)
                                                                                # print(buyr_dict)
                                                                                if money > 0 and money >= cart_mon:
                                                                                    with open(user_buy,
                                                                                              'wb') as user_buy_file:  # 把购物车里的物品更新到已购买列表里去
                                                                                        pickle.dump(buyr_dict,
                                                                                                    user_buy_file)
                                                                                    cartr_mer.clear()
                                                                                    with open(user_home,
                                                                                              'rb') as homer_file:
                                                                                        home_dict = pickle.load(
                                                                                            homer_file)
                                                                                        money = home_dict[user_name][
                                                                                            'have_money']
                                                                                        money = money - cart_mon
                                                                                        home_dict[user_name][
                                                                                            'have_money'] = money
                                                                                        # print(money)
                                                                                        # print(home_dict)
                                                                                    with open(user_home,
                                                                                              'wb') as homew_file:
                                                                                        home_dict = pickle.dump(
                                                                                            home_dict, homew_file)
                                                                                    with open(cart,
                                                                                              'wb') as cart_clear_file:
                                                                                        pickle.dump(cartr_dict,
                                                                                                    cart_clear_file)
                                                                                        buy_msg = "购买成功，请到已购买中查看".center(
                                                                                            30, '+')
                                                                                        print('\n')
                                                                                        print(buy_msg)
                                                                                        print('\n')
                                                                                else:
                                                                                    print("\n对不起您的余额不足，请充值\n")
                                                                                    out_flag = True
                                                                            else:
                                                                                print("\n没有商品，请您到购物列表中挑选商品\n")
                                                                                out_flag = True

                                                                        elif cart_chiose == 'c':
                                                                            user_mer = cartr_dict[user_name]['mer']
                                                                            user_mer.clear()
                                                                            with open(cart, 'wb') as cartw_file:
                                                                                pickle.dump(cartr_dict, cartw_file)
                                                                        elif cart_chiose == 'b':
                                                                            out_flag = True
                                                                        else:
                                                                            print("输入有误请重新输入")
                                                                            break
                                                                    else:
                                                                        print("请重新输入")
                                                                        continue
                                                            #=================================================================
                                                            else:                                           #如果输入有误则退出到功能菜单
                                                                exit_flag = True
                                                    elif frist_choice == 2:                             #已购买
                                                        with open(user_buy,'rb') as buyr_file:          #获取已购买的物品及已经花出的金额
                                                           user_history=pickle.load(buyr_file)
                                                           his_mer_list=user_history[user_name]['mer']
                                                           out_mon_list=user_history[user_name]['out_money']
                                                          # print(his_mer_list)
                                                        with open(user_home,'rb') as cartr_file:                #打开用户信息表
                                                            homer_dict=pickle.load(cartr_file)
                                                            his_mon_list=homer_dict[user_name]['have_money']      #提取现有金额


                                                        if his_mer_list=={}:
                                                           print("\n天哪！你还什么都没有够买!\n")
                                                           out_flag=True
                                                        else:
                                                            for mer_list in his_mer_list:
                                                                # print(mer_list,his_mer_list[mer_list])
                                                                show_buy=("%s"%mer_list).center(30,"=")
                                                                print(show_buy)
                                                                print("已购买商品    数量     价格")
                                                                for key in his_mer_list[mer_list]:
                                                                    print(key,'\t\t',his_mer_list[mer_list][key][0],'\t\t',his_mer_list[mer_list][key][1])

                                                                #print(his_mer_list[mer_list])
                                                                money=("您的余额:%s"%his_mon_list).center(30,'=')
                                                            print(money)
                                                            out_input=input("请输入【b】键返回：")
                                                            if out_input is not None:
                                                               out_flag=True

                                                    elif frist_choice == 3:
                                                        out_flag=False
                                                        while not out_flag:
                                                            with open(user_home,'rb') as homer_file:
                                                                home_dict=pickle.load(homer_file)
                                                                money=home_dict[user_name]['have_money']
                                                            with open(cart,'rb') as cartr_file:
                                                                cartr_dict=pickle.load(cartr_file)
                                                                #print(cartr_dict)
                                                                cart_mon=0
                                                            with open(user_buy,'rb') as buyr_file:
                                                                buyr_dict=pickle.load(buyr_file)

                                                                for user_mer in cartr_dict[user_name]['mer']:
                                                                    cart_mer=cartr_dict[user_name]['mer'][user_mer]
                                                                    print(user_mer,cart_mer)
                                                                    cart_mon+=cart_mer[1]
                                                                money_msg=("您的余额为【%s】,购物车内物品金额【%s】"%(money,cart_mon)).center(30,'+')
                                                                print(money_msg)
                                                                print('\n')
                                                            cart_chiose=input("输入【y】结账，输入【c】清空看购物车，输入【b】退出")
                                                            if cart_chiose.isalnum():
                                                                cart_chiose=cart_chiose.strip()
                                                                if cart_chiose == 'y':
                                                                    cartr_mer = cartr_dict[user_name]['mer']
                                                                    #print(cartr_mer)
                                                                    if cartr_mer!={}:
                                                                        buy_mer=buyr_dict[user_name]['mer']
                                                                        buy_time=time.time()
                                                                        local_time=time.localtime(buy_time)
                                                                        show_time=time.strftime('%Y-%m-%d %H:%M:%S',local_time)
                                                                        new_mer={show_time:cartr_mer}
                                                                        buy_mer.update(new_mer)
                                                                        # print(buyr_dict)
                                                                        if money >0 and money>= cart_mon :
                                                                            with open(user_buy,'wb') as user_buy_file:   #把购物车里的物品更新到已购买列表里去
                                                                                pickle.dump(buyr_dict,user_buy_file)
                                                                            cartr_mer.clear()
                                                                            with open(user_home, 'rb') as homer_file:
                                                                                home_dict = pickle.load(homer_file)
                                                                                money = home_dict[user_name]['have_money']
                                                                                money=money-cart_mon
                                                                                home_dict[user_name]['have_money']=money
                                                                                # print(money)
                                                                                # print(home_dict)
                                                                            with open(user_home, 'wb') as homew_file:
                                                                                home_dict = pickle.dump(home_dict,homew_file)
                                                                            with open(cart,'wb') as cart_clear_file:
                                                                                pickle.dump(cartr_dict,cart_clear_file)
                                                                                buy_msg="购买成功，请到已购买中查看".center(30,'+')
                                                                                print('\n')
                                                                                print(buy_msg)
                                                                        else:
                                                                            print("\n对不起您的余额不足，请充值\n")
                                                                            out_flag=True
                                                                    else:
                                                                        print("\n没有商品，请您到购物列表中挑选商品\n")
                                                                        out_flag=True

                                                                elif cart_chiose == 'c':
                                                                    user_mer=cartr_dict[user_name]['mer']
                                                                    user_mer.clear()
                                                                    with open(cart,'wb') as cartw_file:
                                                                        pickle.dump(cartr_dict,cartw_file)
                                                                elif cart_chiose == 'b':
                                                                    out_flag=True
                                                                else:
                                                                    print("输入有误请重新输入")
                                                                    break
                                                            else:
                                                                print("请重新输入")
                                                                continue


                                                    elif frist_choice == 4:
                                                        chose_flag=False
                                                        while not chose_flag:
                                                            with open(user_home, 'rb') as userr_file:
                                                                home_dict = pickle.load(userr_file)
                                                                his_mon = home_dict[user_name]['have_money']
                                                            mon_mes=("您当前的金额为%s"%his_mon).center(20,'-')
                                                            print('\n')
                                                            print(mon_mes)
                                                            mon_num=input("\n输入您要充值的【金额】或者按【b】退出： ")
                                                            if mon_num.isdigit():
                                                                mon_num=int(mon_num)
                                                                with open(user_home,'rb') as userr_file:
                                                                    home_dict=pickle.load(userr_file)
                                                                    home_dict[user_name]['have_money']=home_dict[user_name]['have_money']+mon_num
                                                                #print(home_dict)

                                                                with open(user_home,'wb') as userw_file:
                                                                    pickle.dump(home_dict,userw_file)
                                                                continue
                                                            elif mon_num=='b':
                                                                chose_flag=True
                                                            else:
                                                                print('您输入的有误，请重新输入')
                                                                continue
                                                else:                                               #功能列表处选择错误后让其重新选择。
                                                    print("输入有误请重新输入：")
                                                    continue
                                            exit()
                                        else:
                                            print('对不起您的输入有错误，请重新输入。')
                                            lock_dict[user_name]=lock_count-1
                                            with open(user_lock,'wb') as wlock_file:
                                                pickle.dump(lock_dict,wlock_file)
                                            continue
                                    else:
                                        print("对不起没有这个用户,请【重新输入】或【注册】")
                                        continue
                                else:
                                    print("您的账户已经被锁定")
                            else:
                                print("对不起没有这个用户，请【重新输入】或【注册】")
                                continue
                        else:
                            print("没有这个用户，请您注册")
                            continue
            if sign2_input!='1':
                break
    elif user_input == '3':
        print('''
            姓名：韩旭
            作业：python13【day 2】

        ''')
        sign3_input=input("返回按【b】")
        if sign3_input=='b':
            continue
    else:
        print("你的输入有错误请重新输入")
        print(user_input)
        continue


        #空的购物车结账后会传入空值     yes
        #输入名称密码中要把空格脱掉
        #已购物品展示                                     yes
        #选择商品后要把已经添加到购物车里的商品也一起显示出来    yes
        #水果区放入购物车有每次选择完商品后要在此循环出商品价格  yes
        #按C 直接进入购物车界面